﻿#region License
// Copyright © 2004 - 2010 All Right Reserved, Derek Goslin
// 
// 1. Definitions
// The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
// A "contribution" is the original software, or any additions or changes to the software.
// A "contributor" is any person that distributes its contribution under this license.
// "Licensed patents" are a contributor's patent claims that read directly on its contribution.
// 
// 2. Grant of Rights
// (A) Copyright Grant- Subject to the terms of this license, including the license
//     conditions and limitations in section 3, each contributor grants you a non-exclusive,
//     worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative 
//     works of its contribution, and distribute its contribution or any derivative works that you create.
// (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations
//     in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its
//     licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of
//     its contribution in the software or derivative works of the contribution in the software.
// 
// 3. Conditions and Limitations
// (A) Reciprocal Grants- For any file you distribute that contains code from the software (in source code or binary format), 
//     you must provide recipients the source code to that file along with a copy of this license, which license will govern that file. 
//     You may license other files that are entirely your own work and do not contain code from the software under any terms you choose.
// (B) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
// (C) If you bring a patent claim against any contributor over patents that you claim are infringed by the software,
//     your patent license from such contributor to the software ends automatically.
// (D) If you distribute any portion of the software, you must retain all copyright, patent, trademark,
//     and attribution notices that are present in the software.
// (E) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy 
//     of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do 
//     so under a license that complies with this license.
// (F) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions.
//    You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your
//    local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
#endregion

using System;
using DnEcuDiag.Ecus.Kwp;
using DnEcuDiag.Factories;
using NUnit.Framework;
using DnEcuDiag.Ecus.Generic;

namespace DnEcuDiag.Tests
{
    [TestFixture]
    public class Abs57Tests
    {
        private KwpEcu _ecu;
        private KwpTestDevice device;

        [TestFixtureSetUp]
        public void Init()
        {
            try
            {
                device = new KwpTestDevice();

                _ecu = EcuFactory.CreateEcu(EcuFactory.EcuList.Find(obj => obj.Name.Equals("Bosch ABS - 5.7"))) as KwpEcu;
                _ecu.DiagnosticInterface = device;
                _ecu.Connect();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        [Test]
        public void TestConnect()
        {
            Assert.AreEqual("Bosch ABS - 5.7", _ecu.Model);
            Assert.AreEqual("0123456789 ", _ecu.DrawingNumber);
            Assert.AreEqual("0123456789 ", _ecu.HardwareNumber);
            Assert.AreEqual("0x10", _ecu.HardwareVersionNumber);
            Assert.AreEqual("0123456789 ", _ecu.SoftwareNumber);
            Assert.AreEqual("0x85170", _ecu.SoftwareVersionNumber);
            Assert.AreEqual("01234 ", _ecu.OmologationNumber);
            Assert.AreEqual("55 AA 55 AA 55 ", _ecu.IsoCode);
            Assert.AreEqual("19 97 03 31 ", _ecu.ProductionDate);
            Assert.AreEqual("19 97 03 31 ", _ecu.ProgrammingDate);
            Assert.AreEqual(0x20, _ecu.EcuAddress);
            Assert.IsNull(_ecu.RpmSensor);
            Assert.IsNull(_ecu.SpeedSensor);
        }

        [Test]
        public void TestSensors()
        {
            Assert.AreEqual(37, _ecu.Sensors.Count);

            TestSensorValues(_ecu);
        }

        [Test]
        public void TestFaults()
        {
            Assert.AreNotEqual(0, _ecu.ErrorMemoryStructureDefinition.ErrorDefinitions.Count);
            Assert.AreEqual(42, _ecu.ErrorMemoryStructureDefinition.ErrorDefinitions.Count);

            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // C1200
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////			
            device.FaultcodeList = new byte[] { 0x58, 01, 0x52, 0x00, 0xE1 };
            device.Faultcodeinfo = new byte[] { 0x57, 1, 0x52, 0x55, 00, 1, 0, 00, 0, 0, 0, 1 };
            _ecu.ReadFaultCodes();

            Assert.AreEqual(1, _ecu.ActiveFaultCodes.Count, "ActiveFaultCodes.Count == 1");
            Console.WriteLine(_ecu.ActiveFaultCodes[0].FaultDescription);
            Assert.AreEqual("C1200", (_ecu.ActiveFaultCodes[0]).FaultComponent);
            Assert.AreEqual(
                "Failed Component Code: ADX Speed sensor, electrical error\r\n" +
                "Fault Symptom: No fault symptom available\r\n" +
                "Warning Lamp Status: Warning Lamp not Illuminated\r\n" +
                "Fault Test State: Test complete for this Error or not applicable.\r\n" +
                "Fault Code Storage State: No Error Detected at time of request\r\n" +
                "Event Count: 1 count\r\n" +
                "Environmental Condition One: Vehicle Speed: 0.06 km/h\r\n" +
                "Environmental Condition Two: System state: Brake pedal not pressed\r\n" +
                "Environmental Condition Two: System state: ASR not disabled by driver\r\n" +
                "Environmental Condition Two: System state: ABS control disabled\r\n" +
                "Environmental Condition Two: System state: ASR control disabled\r\n" +
                "Environmental Condition Two: System state: VDC control disabled\r\n" +
                "Environmental Condition Three: Key on counter: 0\r\n",
                _ecu.ActiveFaultCodes[0].FaultDescription);

            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // one P1772
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            device.FaultcodeList = new byte[] { 0x58, 0x1, 0x52, 0x76, 0xE1 };
            device.Faultcodeinfo = new byte[] { 0x57, 1, 0x57, 0x26, 0x08, 10, 10, 100, 0x33, 10, 10, 1 };
            _ecu.ReadFaultCodes();

            Assert.AreEqual(1, _ecu.ActiveFaultCodes.Count, "ActiveFaultCodes.Count == 1");
            Console.WriteLine(_ecu.ActiveFaultCodes[0].FaultDescription);
            Assert.AreEqual("C1276", (_ecu.ActiveFaultCodes[0]).FaultComponent);
            Assert.AreEqual(
                "Failed Component Code: Valve relay\r\n" +
                "Fault Symptom: Invalid signal\r\n" +
                "Warning Lamp Status: Warning Lamp not Illuminated\r\n" +
                "Fault Test State: Test complete for this Error or not applicable.\r\n" +
                "Fault Code Storage State: No Error Detected at time of request\r\n" +
                "Event Count: 1 count\r\n" +
                "Environmental Condition One: Vehicle Speed: 1600.63 km/h\r\n" +
                "Environmental Condition Two: System state: Brake pedal not pressed\r\n" +
                "Environmental Condition Two: System state: ASR not disabled by driver\r\n" +
                "Environmental Condition Two: System state: ABS control enbled\r\n" +
                "Environmental Condition Two: System state: ASR control disabled\r\n" +
                "Environmental Condition Two: System state: VDC control disabled\r\n" +
                "Environmental Condition Three: Key on counter: 2611\r\n",
                _ecu.ActiveFaultCodes[0].FaultDescription);

            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // one P0725
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            device.FaultcodeList = new byte[] { 0x58, 0x1, 0x07, 0x25, 0xE1 };
            device.Faultcodeinfo = new byte[] { 0x57, 1, 0x07, 0x25, 0x08, 10, 10, 100, 0x33, 10, 10, 1 };
            _ecu.ReadFaultCodes();
        }

        [Test]
        public void TestPerformanceMeasurment()
        {
            Assert.AreEqual(null, _ecu.SpeedSensor);
            Assert.AreEqual(null, _ecu.RpmSensor);
        }

        [Test]
        public void TestActuators()
        {
            Assert.AreEqual(18, _ecu.Actuators.Count);

            foreach (var actuator in _ecu.Actuators)
            {
                actuator.Duration = 1;
                _ecu.ActivateActuator(actuator);
            }
        }

        private void TestSensorValues(Ecu ecu)
        {
            TimeStampedMeasurement tsm;
            LocalIdentifier li;
            int i = 0;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xFF };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0xff * 0.0625f, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0xffff * 0.0625f, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0xffff * 0.0625f, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0xffff * 0.0625f, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("Brake light switch active", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Brake light switch InActive", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("Valve relay On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Valve relay Off", tsm.Status);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;
            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0x0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0xff * 78f, tsm.Measurement);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x7f, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(BitConverter.ToInt16(new byte[] { 0xFF, 0x7F }, 0) * 2.5f, tsm.Measurement);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x80, 0x00 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(BitConverter.ToInt16(new byte[] { 0, 0x80 }, 0) * 2.5f, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x7f, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(BitConverter.ToInt16(new byte[] { 0xff, 0x7f }, 0) * 0.2715f, tsm.Measurement);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x80, 0x00 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(BitConverter.ToInt16(new byte[] { 0, 0x80 }, 0) * 0.2715f, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x7f, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(BitConverter.ToInt16(new byte[] { 0xff, 0x7f }, 0) * 0.3255f, tsm.Measurement);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x80, 0x00 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(BitConverter.ToInt16(new byte[] { 0, 0x80 }, 0) * 0.3255f, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x7f, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(BitConverter.ToInt16(new byte[] { 0xff, 0x7f }, 0) * 0.192f, tsm.Measurement);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x80, 0x00 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(BitConverter.ToInt16(new byte[] { 0, 0x80 }, 0) * 0.192f, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x00, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0xff, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x00, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0xff * 0.39f, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x00, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0xff * 10f, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x00, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0xff * 0.39f, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x00, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0xff * 0.39f, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(0, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x0f };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);

            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);
            Assert.AreEqual(true, tsm.IsStatus);
            Assert.AreEqual("On", tsm.Status);
            Assert.AreEqual(1, tsm.Measurement);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);
            Assert.AreEqual(true, tsm.IsStatus);
            Assert.AreEqual("Off", tsm.Status);
            Assert.AreEqual(0, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x00, 0 };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);
            Assert.AreEqual("Off", tsm.Status);
            Assert.AreEqual(0, tsm.Measurement);

            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0x00, 0xFF };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);
            Assert.AreEqual("On", tsm.Status);
            Assert.AreEqual(1, tsm.Measurement);

            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, ecu.Sensors[i].Name));
            li = ecu.Sensors[i] as LocalIdentifier;
            device.LocalidentifierData = new byte[] { 0x61, (Byte)li.Id, 0xff, 0xff };
            tsm = ecu.ReadSensor(ecu.Sensors[i]);
            Assert.AreEqual(0xffff, tsm.Measurement);

            i++;
        }

        [TestFixtureTearDown]
        public void Dispose()
        {
            if (_ecu != null)
            {
                _ecu.Close();
                _ecu.Dispose();
            }
        }
    }	
}
